package com.example.finaltest.shoppingCart;


import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.finaltest.R;
import com.example.finaltest.utils.URLUtils;
import com.squareup.picasso.Picasso;

import java.util.List;


/**
 * Created by Administrator on 2017/3/26.
 * 购物车适配器
 */

public class ShoppingCartAdapter extends BaseAdapter {
    private List<Goods.GoodsInfo> childrens;
    private Context mcontext;
    private CheckInterface checkInterface;
    private ModifyCountInterface modifyCountInterface;
    private int count = 0;
    int flg;

    public ShoppingCartAdapter(List<Goods.GoodsInfo>childrens, Context mcontext,int flg) {
        this.childrens = childrens;
        this.mcontext = mcontext;
        count = childrens.size();
        this.flg = flg;
    }

    @Override
    public int getCount() {
        return childrens.size();
    }

    @Override
    public Object getItem(int position) {
        return childrens.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.item_shopcat_product,parent,false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        if(flg == 1){
            childViewHolder.singleCheckBox.setVisibility(View.GONE);
        }
        childViewHolder.editGoodsData.setVisibility(View.VISIBLE);
        final Goods.GoodsInfo child = childrens.get(position);
        if (child != null) {
            childViewHolder.goodsName.setText(child.getCommodityName());
            childViewHolder.goodsPrice.setText("￥" + child.getUnivalence()+ "");
//            childViewHolder.goodsImage.setImageResource(R.drawable.cmaz);
            childViewHolder.goodsBuyNum.setText("x" + child.getCount() + "");
            childViewHolder.singleCheckBox.setChecked(child.isChoosed());
            if (!TextUtils.isEmpty(child.getCommodityPhoto())) {
                String photourl = URLUtils.PUBLIC_URL+child.getCommodityPhoto();
                Picasso.with(mcontext).load(photourl).into(childViewHolder.goodsImage);
            }

            childViewHolder.singleCheckBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    child.setChoosed(((CheckBox) v).isChecked());
                    childViewHolder.singleCheckBox.setChecked(((CheckBox) v).isChecked());
                    checkInterface.checkChild(position, ((CheckBox) v).isChecked());
                }
            });

        }
        return convertView;
    }






    public CheckInterface getCheckInterface() {
        return checkInterface;
    }

    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    public ModifyCountInterface getModifyCountInterface() {
        return modifyCountInterface;
    }

    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    /**
     * 店铺的复选框
     */
    public interface CheckInterface {

        void checkChild(int childPosition, boolean isChecked);
    }


    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        void doUpdate(int childPosition,View showCountView);
    }




    static class ChildViewHolder {
        CheckBox singleCheckBox;
        ImageView goodsImage;
        TextView goodsName;
        TextView goodsPrice;
        TextView goodsBuyNum;
        LinearLayout editGoodsData;
        RelativeLayout amount;
        public ChildViewHolder(View view) {
            amount = view.findViewById(R.id.amount);
            singleCheckBox= view.findViewById(R.id.single_checkBox);
            goodsImage= view.findViewById(R.id.goods_image);
            goodsName= view.findViewById(R.id.goods_name);
            goodsPrice = view.findViewById(R.id.goods_price);
            goodsBuyNum = view.findViewById(R.id.goods_buyNum);
            editGoodsData = view.findViewById(R.id.edit_goods_data);
        }
    }
}

